package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.User;
import tools.SendEmail;
import utils.db.impl.EditDevice;
import utils.db.impl.EditUser;
import utils.db.impl.UserFetcher;

/**
 * Servlet implementation class DeleteUserServlet
 */
@WebServlet("/DeleteUserServlet")
public class DeleteUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	HttpSession session;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("delete"));
		if(id!=0){
			EditUser edit = new EditUser();
			UserFetcher u = new UserFetcher();
			User user = u.getUserById(id);
			EditDevice device = new EditDevice();
			String name = user.getFirstName();
			String email=user.getEmail();
			SendEmail.sendDeleteEmail(email,name);
			edit.deleteUser(id);
			device.deleteDeviceByClientID(id);
			session = request.getSession(true);
			session.setAttribute("delete", "Użytkownik został usunięty!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelUsers#add");
		}else{
			session = request.getSession(true);
			session.setAttribute("delete", "Błąd przy usuwaniu użytkownika!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanelUsers#add");
		}
	}

}
